const dd = new Date();

import { getBill } from '../api/index';

import Vue from 'vue';

const vm = new Vue();

const state = {
    year: dd.getFullYear(),
    monthlyInfo: [],
};

const actions = {
    async updateYear(context, data) {
        state.monthlyInfo.splice(0);
        console.log(vm);
        vm.$showLoading();
        const infos = [];
        for (let i = 1; i <= 12; i++) {
            let result = await getBill({ year: data, month: i }, sessionStorage.getItem('loginToken') ? sessionStorage.getItem('loginToken') : '');
            if (!result.success) {
                alert('数据请求失败！');
                vm.$hideLoading();
                context.commit('SPLICEMONTHLYINFO');
            } else {
                // console.log(result);
                infos.push({ month: i, get: result.result.month_get_money, pay: result.result.month_pay_money, year: data });
            }
        }
        vm.$hideLoading();
        context.commit('UPDATEYEAR', infos);
    }




};

const mutations = {
    UPDATEYEAR(state, value) {
        state.year = value[0].year;
        for (let i = 0; i < value.length; i++) {
            state.monthlyInfo.push(value[i]);
        }
        console.log(state.monthlyInfo);
    },
    SPLICEMONTHLYINFO(state) {
        state.monthlyInfo.splice(0);
    }
};

const getters = {

};

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
}